package com.may.springboot.controller;

import com.alibaba.fastjson.JSONObject;
import com.may.springboot.entity.Student;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author mayuanbao
 * @date 2020/3/12 11:37
 */
@RestController
public class DemoController {


    /**
     * @description @RequestParam
     * @author mayuanbao
     * @date 2020/3/12 17:09
     * @param id id
     * @param name name
     * @return java.lang.String
     */

    @GetMapping("/getStud")
    public String getStud(@RequestParam("id") String id, @RequestParam("name") String name) {
        return id + ": " + name;
    }

    /**
     * @param id id
     * @return java.lang.String
     * @description @PathVariable
     * @author mayuanbao
     * @date 2020/3/12 14:55
     */
    @GetMapping("/getId/{id}")
    public String getId(@PathVariable("id") String id) {
        return id;
    }

    /**
     * @param map map
     * @return java.lang.String
     * @description @RequestBody,map接收
     * @author mayuanbao
     * @date 2020/3/12 14:55
     */
    @PostMapping("/getStuds")
    public String getStuds(@RequestBody Map<String, String> map) {
        return map.toString();
    }

    /**
     * @param student student
     * @return java.lang.String
     * @description @RequestBody，javabean接收
     * @author mayuanbao
     * @date 2020/3/12 15:43
     */
    @PostMapping("/getStudent")
    public String getStudent(@RequestBody Student student) {
        return student.toString();
    }

    /**
     * @param jsonObject jsonObject
     * @return java.lang.String
     * @description @RequestBody，JSONObject接收
     * @author mayuanbao
     * @date 2020/3/12 15:38
     */
    @PostMapping("/getJson")
    public String getJson(@RequestBody JSONObject jsonObject) {
        return jsonObject.toJSONString();
    }

    /**
     * @param str str
     * @return java.lang.String
     * @description @RequestBody
     * @author mayuanbao
     * @date 2020/3/12 15:41
     */
    @PostMapping("/getJsons")
    public String getJsons(@RequestBody String str) {
        return str;
    }

    /**
     * @param student student
     * @return java.lang.String
     * @description 无注解
     * @author mayuanbao
     * @date 2020/3/12 15:44
     */
    @PostMapping("/getStudents")
    public String getStudents(Student student) {
        return student.toString();
    }

    /**
     * @Description @RequestHeader获取header中自定义参数，@CookieValue获取Cookie中自定义参数
     * @Author mayuanbao
     * @Date 2020/3/12 17:23
     * @Param [myHeader, myCookie]
     * @return java.lang.String
     */
    @GetMapping("/header")
    public String getHeader(@RequestHeader("myHeader") String myHeader, @CookieValue("myCookie") String myCookie) {
        return myHeader + "-" + myCookie;
    }
}
